package cn.wolfcode.business.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.wolfcode.business.util.DateUtil;
import cn.wolfcode.business.util.RegexUtils;
import cn.wolfcode.business.util.VehiclePlateNoUtil;
import cn.wolfcode.common.utils.DateUtils;
import jdk.jshell.execution.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.BusAppointmentMapper;
import cn.wolfcode.business.domain.BusAppointment;
import cn.wolfcode.business.service.IBusAppointmentService;

/**
 * 养修信息预约Service业务层处理
 * 
 * @author lyh
 * @date 2025-09-18
 */
@Service
public class BusAppointmentServiceImpl implements IBusAppointmentService 
{
    @Autowired
    private BusAppointmentMapper busAppointmentMapper;

    /**
     * 查询养修信息预约
     * 
     * @param id 养修信息预约主键
     * @return 养修信息预约
     */
    @Override
    public BusAppointment selectBusAppointmentById(Long id)
    {
        return busAppointmentMapper.selectBusAppointmentById(id);
    }

    /**
     * 查询养修信息预约列表
     * 
     * @param busAppointment 养修信息预约
     * @return 养修信息预约
     */
    @Override
    public List<BusAppointment> selectBusAppointmentList(BusAppointment busAppointment)
    {
        return busAppointmentMapper.selectBusAppointmentList(busAppointment);
    }

    /**
     * 新增养修信息预约
     * 
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int insertBusAppointment(BusAppointment busAppointment)
    {
        //1.前台和后台的验证后端都要验证一次，也叫合理化验证
        if(busAppointment==null)
        {
            throw new RuntimeException("参数错误");
        }
          if(busAppointment.getCustomerName()==null||busAppointment.getCustomerPhone()== null
           ||busAppointment.getAppointmentTime()==null||busAppointment.getLicensePlate()==null
          ||busAppointment.getCarSeries()==null||busAppointment.getServiceType()==null){
              throw new RuntimeException("非法参数");
          }
          //2.联系方式合理话验证
        if(!RegexUtils.isPhoneLegal(busAppointment.getCustomerPhone())){
            throw new RuntimeException("手机号码格式错误");
        }
         //3.车牌号合理验证
        if(VehiclePlateNoUtil.getVehiclePlateNo(busAppointment.getLicensePlate())==null){
            throw new RuntimeException("车牌号格式错误");
        }
        //4.预约时间合理验证
        Date date = DateUtil.changeDate(new Date(), Calendar.MINUTE, 1);
        if(busAppointment.getAppointmentTime().before(date)){
            throw new RuntimeException("预约时间不能早于当前时间");
        }
        //5.关键字段校验
        //若不为了扩展 那么存在偷懒一种方式
        busAppointment.setStatus(BusAppointment.STATUS_BOOKING);
        busAppointment.setCreateTime(DateUtils.getNowDate());
        return busAppointmentMapper.insertBusAppointment(busAppointment);
    }
    /**
     * 修改养修信息预约
     * 
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int updateBusAppointment(BusAppointment busAppointment)
    {
          //1.前台和后台的验证后端都要验证一次，也叫合理化验证
        if(busAppointment==null){
            throw new RuntimeException("参数错误");
        }
        //2.联系方式合理话验证

        if(!RegexUtils.isPhoneLegal(busAppointment.getCustomerPhone())){
            throw new RuntimeException("手机号码格式错误");
        }
        //3.车牌号合理验证
        if(VehiclePlateNoUtil.getVehiclePlateNo(busAppointment.getLicensePlate())==null){
            throw new RuntimeException("车牌号格式错误");
        }
        //4.预约时间合理验证
        Date date= DateUtil.changeDate(new Date(), Calendar.MINUTE, 1);
        if(busAppointment.getAppointmentTime().before(date)){
            throw new RuntimeException("预约时间不能早于当前时间");
        }
        //5.状态必须是预约状态
        //所以这些状态不允许修改的都需要安全验证
        //根据ID将原始数据查询出来
        BusAppointment oldobj = this.selectBusAppointmentById(busAppointment.getId());
        if(oldobj==null){
            throw new RuntimeException("参数错误");
        }
        if(!busAppointment.getStatus().equals(oldobj.getStatus())){
            throw new RuntimeException("非法数据");

        }
        //在判断用户的状态是预约中才可以修改操作
        if (!BusAppointment.STATUS_BOOKING.equals(oldobj.getStatus())) {
            throw new RuntimeException("状态必须是预约中才可以编辑操作 ");
        }



        return busAppointmentMapper.updateBusAppointment(busAppointment);
    }

    /**
     * 批量删除养修信息预约
     * 
     * @param ids 需要删除的养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentByIds(Long[] ids)
    {    Long id = ids[0];
        if(id==null){
            throw new RuntimeException("非法参数");
        }
        BusAppointment busAppointment = this.selectBusAppointmentById(id);
        if(busAppointment==null){
            throw new RuntimeException("参数错误");
        }
        //验证状态
        if(!BusAppointment.STATUS_BOOKING.equals(busAppointment.getStatus())){
            throw new RuntimeException("状态必须是预约中才可以删除操作");
        }

        return busAppointmentMapper.deleteBusAppointmentByIds(ids);
    }

    /**
     * 删除养修信息预约信息
     * 
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentById(Long id)
    {
        return busAppointmentMapper.deleteBusAppointmentById(id);
    }

    @Override
    public int Arrival(Long id) {
        //1.参数合理化验证
        if(id==null){
            throw new RuntimeException("参数错误");
        }
        BusAppointment busAppointment = this.selectBusAppointmentById(id);
        if(busAppointment==null){
            throw new RuntimeException("参数错误");
        }
        //2.判断状态是预约中才能到店
        if (!BusAppointment.STATUS_BOOKING.equals(busAppointment.getStatus())) {
            throw new RuntimeException("状态必须是预约中才可以到店");
        }

        //3.设置到店时间和更改状态为到店中
        return busAppointmentMapper.changeStatusAndArrivalTimeById(new Date(),BusAppointment.STATUS_ARRIVED,id);

    }

    @Override
    public int cancel(Long id) {
        //1.参数合理化验证
        if(id==null){
            throw new RuntimeException("参数错误");
        }
        BusAppointment busAppointment = this.selectBusAppointmentById(id);
        if(busAppointment==null){
            throw new RuntimeException("非法参数");
        }
        //2.判断状态是预约中才能取消
        if(!BusAppointment.STATUS_BOOKING.equals(busAppointment.getStatus())){
            throw new RuntimeException("状态必须是预约中才可以取消");
        }
        return  busAppointmentMapper.changeStatusById(BusAppointment.STATUS_USER_CANCELLED,id);
    }
}
